package com.example.zqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zqh.mapper.FoodMapper;
import com.example.zqh.mapper.UserMapper;
import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.Food;
import com.example.zqh.pojo.Purchase;
import com.example.zqh.mapper.PurchaseMapper;
import com.example.zqh.pojo.User;
import com.example.zqh.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zqh.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.security.Principal;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-09-10
 */
@Service
public class OrderServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements OrderService {

    @Autowired
    PurchaseMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    FoodMapper foodMapper;




    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean generateAnOrder(Integer number, Integer food_id, Principal principal) {
        String name = principal.getName();
        //获取购买者信息
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        AssertUtil.isTrue(user == null,"参数传递错误");

        //获取食物信息
        Food food = foodMapper.selectById(food_id);
        AssertUtil.isTrue(food == null, "参数传递错误");

        //判断用户余额是否足够
        Double title = food.getPrice() * number;
        Double isT = user.getMoney() - title;
        AssertUtil.isTrue(isT < 0,"余额不足");

        //足了以后，用户的钱要变少
        user.setMoney(isT);
        int i = userMapper.updateById(user);
        AssertUtil.isTrue(i != 1,"添加订单失败");

        Purchase order = new Purchase();
        order.setNumber(number)
                .setPayment(1)
                .setMoney(title)
                .setFoodId(food.getId())
                .setUserId(user.getId());

        int insert = orderMapper.insert(order);
        AssertUtil.isTrue(insert != 1,"生成订单失败");



        return RespBean.success("订单生成成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 取消订单，退款
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean cancelOrder(Integer id, Principal principal) {
        String name = principal.getName();
        AssertUtil.isTrue(name == null,"参数传递错误");
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        //查询订单信息
        Purchase order = orderMapper.selectById(id);
        AssertUtil.isTrue(order == null, "该订单不存在");

        //把订单花费返回加到用户的余额里面去
        double v = user.getMoney() + order.getMoney();
        user.setMoney(v);
        int i = userMapper.updateById(user);
        AssertUtil.isTrue(i != 1, "退款失败");

        //删除订单表里该订单的数据
        int i1 = orderMapper.deleteById(id);
        AssertUtil.isTrue(i1 != 1, "退款失败");

        return RespBean.success("退款成功");
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 确定订单以后，该订单将会在数据库中消失（相当于删除订单）
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean deleteOrderInterface(Integer id) {
        int i = orderMapper.deleteById(id);
        AssertUtil.isTrue(i != 1, "确认送达失败");
        return RespBean.success("确认送达成功");
    }


    /**
     * @Author: zqh
     * @Date:
     * @Description: 查询属于自己的订单
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public RespBean inquireAboutYourOrder(Principal principal) {
        String name = principal.getName();
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("name", name));
        AssertUtil.isTrue(user == null,"参数传递错误");

        //查询用户的所有订单
        List<Purchase> orders = orderMapper.selectList(new QueryWrapper<Purchase>().eq("user_id", user.getId()));
        return RespBean.success("查询成功",orders);
    }


}
